# -*- coding=utf-8-*-
"""
@File:memory_selection
@Author:Created by Han X.Y
@Date:on 2021/7/12 18:00 
"""

"""
给定一个赎金信 (ransom) 字符串和一个杂志(magazine)字符串，判断第一个字符串 ransom 能不能由第二个字符串 magazines 里面的字符构成。如果可以构成，返回 true ；否则返回 false。

(题目说明：为了不暴露赎金信字迹，要从杂志上搜索各个需要的字母，组成单词来表达意思。杂志字符串中的每个字符只能在赎金信字符串中使用一次。)

来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/ransom-note
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。


输入：ransomNote = "a", magazine = "b"
输出：false

输入：ransomNote = "aa", magazine = "ab"
输出：false

输入：ransomNote = "aa", magazine = "aab"
输出：true


"""


class Solution:
    def canConstruct(self, ransomNote, magazine):
        """

        待优化====================
        """
        list_magazine=list(magazine)
        tmp=[]
        for i in ransomNote:
            tmp.append(i)
            if i in list_magazine:
                list_magazine.remove(i)
                tmp.remove(i)
            else:
                return False
            # print(f'i={i},list_magazine={list_magazine},tmp={tmp}')
        return True if len(tmp)==0 else False

if __name__=='__main__':
    rand=["a",'aa','aa']
    mag=['b','ab','aab']
    s=Solution()
    for i,j in zip(rand,mag):
        print(s.canConstruct(i,j))

